﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Bookvar.ObjectModel;
using Bookvar.ServiceModel;

namespace Bookvar.UI.Adorners
{
    public class AdornerService : IAdornerService
    {
        private readonly MindMapPanel mapPanel;

        public AdornerService( MindMapPanel mapPanel )
        {
            this.mapPanel = mapPanel;
        }

        #region Properties

        public AdornerLayer AdornerLayer
        {
            get { return AdornerLayer.GetAdornerLayer( mapPanel ); }
        }

        #endregion

        #region IAdornerService Members

        public IAdorner AdornTopic( Topic topic )
        {
            return AdornTopic( topic, Colors.Red );
        }

        public IAdorner AdornTopic( Topic topic, Color color )
        {
            TopicContentPresenter presenter = mapPanel.FindTopicContentPresenter( topic.ID );
            if ( presenter != null )
            {
                AdornerContainer adorner = new AdornerContainer( presenter, CreateDefaultTopicAdorner( color ) );
                AdornerLayer.Add( adorner );
                return adorner;
            }
            return null;
        }

        public IAdorner AdornTopic( Topic topic, UIElement adorningElement )
        {
            TopicContentPresenter presenter = mapPanel.FindTopicContentPresenter( topic.ID );
            if ( presenter != null )
            {
                AdornerContainer adorner = new AdornerContainer( presenter, adorningElement );
                AdornerLayer.Add( adorner );
                return adorner;
            }
            return null;
        }

        public IAdorner AdornTopic(
            Topic topic,
            UIElement adorningElement,
            AdornerPosition position,
            AdornerPlacement placement )
        {
            TopicContentPresenter presenter = mapPanel.FindTopicContentPresenter( topic.ID );
            if ( presenter != null )
            {
                TopicAdorner adorner = new TopicAdorner( presenter, adorningElement, position, placement );
                AdornerLayer.Add( adorner );
                return adorner;
            }
            return null;
        }

        public void AddAdorner( IAdorner adorner )
        {
            Adorner newAdorner = adorner as Adorner;
            if ( newAdorner != null )
            {
                AdornerLayer.Add( newAdorner );
            }
        }

        public void RemoveAdorner( IAdorner adorner )
        {
            Adorner removed = adorner as Adorner;
            if ( removed != null )
            {
                AdornerLayer.Remove( removed );
            }
        }

        #endregion

        private static UIElement CreateDefaultTopicAdorner( Color color )
        {
            Border border = new Border();
            border.BorderThickness = new Thickness( 2 );
            border.CornerRadius = new CornerRadius( 7 );
            border.Padding = new Thickness( 2 );

            Color borderColor = color;
            borderColor.ScA = .5f;
            border.BorderBrush = new SolidColorBrush( borderColor );
            border.IsHitTestVisible = false;

            return border;
        }
    }
}